<template>
  <div class="login-container">
    <div class="login-box">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <div class="title-container">
          <h3 class="title"><img :src="image" alt=""></h3>
        </div>

        <el-form-item prop="username">
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="Username"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          >
            <span slot="prefix" class="svg-container">
              <svg-icon icon-class="user" />
            </span>
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="Password"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          >
            <span slot="prefix" class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <span slot="suffix" class="show-pwd" @click="showPwd">
              <svg-icon
                :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
              /> </span></el-input>
        </el-form-item>

        <div class="tips">
          <span style="margin: 20px 10px 20px 0"><input id="" type="checkbox" name=""></span>
          <span>记住密码</span>
        </div>
        <el-button
          :loading="loading"
          type="warning"
          style="width: 100%; margin-bottom: 30px; margin-top: 20px; color: #000;"
          @click.native.prevent="handleLogin"
        >立即登录</el-button>

      </el-form>
      <div class="pic">
        <img
          class="img"
          src="http://www-wms-java.itheima.net/img/contentBg.1321d126.png"
          alt=""
        >
      </div>
    </div>
  </div>
</template>

<script>
import LoGo from '@/assets/images/logo@2x.png'
export default {
  name: 'Login',
  data() {
    return {
      image: LoGo,
      loginForm: {
        username: 'admin',
        password: '111111'
      },
      loginRules: {
        username: [
          { required: true, message: '此项必填', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '此项必填', trigger: 'blur' }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(async(valid) => {
        if (!valid) return
        await this.$store.dispatch('user/login', this.loginForm)
        this.$router.push('/')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  .title-container {
    width: 358px;
    display: flex;
    justify-content: center;
    align-items: center;
    .title{
      margin-top: 72px;
      width: 150px;
      height: 72px;
      text-align: center;
      img{
           width: 100%;
      }
    }
  }
  .login-box {
    box-shadow: 3px 3px 15px 10px #c4c4c4;
    border-radius: 30px;
    display: flex;
    flex-wrap: wrap;
    .login-form {
      height: 516px;
      padding: 0 30px;
    }
    .pic {
      width: 560px;
      height: 516px;
      .img {
        width: 100%;
      }
    }
  }
}
</style>
